package net.uhuijia.goods.goodbanner.service.impl;

import net.uhuijia.constants.DBConst;
import net.uhuijia.enums.GoodBannerType;
import net.uhuijia.vo.good.GoodBannerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.goods.goodbanner.persistence.dao.GoodBannerDao;
import net.uhuijia.goods.goodbanner.persistence.model.GoodBanner;
import net.uhuijia.goods.goodbanner.persistence.model.GoodBannerExample;
import net.uhuijia.goods.goodbanner.persistence.model.GoodBannerExample.Criteria;
import net.uhuijia.goods.goodbanner.service.GoodBannerService;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class GoodBannerServiceImpl
     extends CrudService<GoodBannerDao,GoodBanner,GoodBannerExample>
     implements  GoodBannerService {
	
	@Autowired
	private GoodBannerService goodBannerService;

	@Override
	public GoodBanner addBanner(String goodId, String image, String type) {
		GoodBanner goodBanner = new GoodBanner();
		this.initDefalutFields(goodBanner);
		goodBanner.setGoodId(goodId);
		goodBanner.setImage(image);
		goodBanner.setType(type);
		goodBannerService.add(goodBanner);
		return goodBanner;
	}
	/**
	 * @Author xzj
	 * @Description //根据spuId查询商品轮播图集合
	 * @Date  2018/10/6
	 * @Param
	 * @return
	 **/
	@Override
	public List<GoodBanner> findGoodBannerBySpuId(String spuId) {
		GoodBannerExample example = new GoodBannerExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andGoodIdEqualTo(spuId);
		List<GoodBanner> goodBanners = this.list(example);
		return goodBanners;
	}
	/**
	 * @Author xzj
	 * @Description //根据spuId查询商品轮播图集合 转换成GoodBannerVo
	 * @Date  2018/10/6
	 * @Param
	 * @return
	 **/
	@Override
	public List<GoodBannerVo> findGoodBannerVoBySpuId(String spuId) {
		List<GoodBanner> goodBanners = this.findGoodBannerBySpuId(spuId);
		List<GoodBannerVo> goodBannerVos = new ArrayList<>();
		for(GoodBanner goodBanner : goodBanners){
			GoodBannerVo goodBannerVo = new GoodBannerVo();
			GoodBannerType goodBannerType = GoodBannerType.getGoodBannerType(goodBanner.getType());
			goodBannerVo.setType(goodBannerType);
			goodBannerVo.setUrl(goodBanner.getImage());
			goodBannerVos.add(goodBannerVo);
		}
		return goodBannerVos;
	}
}